package com.hskn.hss.module.empPlan.vo;

import com.hskn.hss.core.annotation.Excel;
import com.hskn.hss.module.empPlan.entity.EmpPlan;
import com.hskn.hss.module.empPlanDate.entity.EmpPlanDate;
import com.hskn.hss.module.empPlanEmployee.entity.EmpPlanEmployee;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作业计划导出
 *
 * @author zcy
 * @date 2021/10/9 13:21
 */
@Data
@ApiModel(value = "作业计划导出-VO", description = "作业计划导出-VO")
public class EmpPlanForExcelVO implements Serializable {
    @ApiModelProperty(value = "计划名称", name = "planName")
    @Excel(name = "计划名称(必填)", align = Excel.Align.CENTER, height = 100, width = 34)
    private String planName;
    @ApiModelProperty(value = "网格名称", name = "gridName")
    @Excel(name = "网格名称(必填)", align = Excel.Align.CENTER, height = 100, width = 24)
    private String gridName;
    @ApiModelProperty(value = "作业类型", name = "workTypeName")
    @Excel(name = "作业类型(必填)", align = Excel.Align.CENTER, height = 100, width = 14)
    private String workTypeName;
    @ApiModelProperty(value = "作业人员", name = "empName")
    @Excel(name = "作业人员(必填)", align = Excel.Align.CENTER, height = 100, width = 120, prompt="注：人员名称用“、”中文顿号分隔开，否则将会导致导入错误")
    private String empName;
    @ApiModelProperty(value = "班组名称", name = "workGroupName")
    @Excel(name = "班组名称(必填)", align = Excel.Align.CENTER, height = 100, width = 24)
    private String workGroupName;
    @ApiModelProperty(value = "排班日期", name = "workDays")
    @Excel(name = "排班日期(必填)", align = Excel.Align.CENTER, height = 100, width = 24, color = IndexedColors.ROYAL_BLUE, prompt="注：日期用“至”中文隔开，如“2021-01-01至2021-02-01”。")
    private String workDays;

    public static EmpPlanForExcelVO buildEmpPlanForExcelVO(EmpPlan empPlan, Map<String, Grid> gridMap
            , Map<String, TlEmployee> employeeMap, Map<String, TlOperation> operationMap, Map<String, WorkGroup> workGroupMap) {
        EmpPlanForExcelVO empPlanForExcelVO = new EmpPlanForExcelVO();
        String planName = "";
        String gridName = "";
        String workTypeName = "";
        String workGroupName = "";
        String empName = "";
        String workDays = "";
        planName = empPlan.getPlanName();
        if (StringUtils.isNotEmpty(empPlan.getGridId())) {
            gridName = gridMap.get(empPlan.getGridId()).getName();
        }
        if (CollectionUtils.isNotEmpty(empPlan.getEmpPlanEmployeeList())) {
            workTypeName = checkWorkTypeName(empPlan.getEmpPlanEmployeeList(), employeeMap, operationMap);
            empName = checkEmpName(empPlan.getEmpPlanEmployeeList(), employeeMap);
        }
        if (CollectionUtils.isNotEmpty(empPlan.getEmpPlanDateList())) {
            workDays = checkDates(empPlan.getEmpPlanDateList());
            workGroupName = checkWorkGroupName(empPlan.getEmpPlanDateList(),workGroupMap);
        }
        empPlanForExcelVO.setPlanName(planName);
        empPlanForExcelVO.setGridName(gridName);
        empPlanForExcelVO.setWorkGroupName(workGroupName);
        empPlanForExcelVO.setWorkTypeName(workTypeName);
        empPlanForExcelVO.setEmpName(empName);
        empPlanForExcelVO.setWorkDays(workDays);
        return empPlanForExcelVO;
    }

    private static String checkWorkTypeName(List<EmpPlanEmployee> empPlanEmployeeList, Map<String, TlEmployee> employeeMap, Map<String, TlOperation> operationMap) {
        for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
            if (employeeMap.containsKey(empPlanEmployee.getEmployeeId())) {
                if (operationMap.containsKey(employeeMap.get(empPlanEmployee.getEmployeeId()).getPersontype())) {
                    return operationMap.get(employeeMap.get(empPlanEmployee.getEmployeeId()).getPersontype()).getOperationtype();
                }
            }
        }
        return "道路保洁";
    }

    private static String checkEmpName(List<EmpPlanEmployee> empPlanEmployeeList, Map<String, TlEmployee> employeeMap) {
        String str = "";
        for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
            if (employeeMap.containsKey(empPlanEmployee.getEmployeeId())) {
                str += employeeMap.get(empPlanEmployee.getEmployeeId()).getName() + "、";
            }
        }
        if (StringUtils.isNotEmpty(str)) {
            str = str.substring(0, str.lastIndexOf("、"));
        }
        return str;
    }

    private static String checkDates(List<EmpPlanDate> empPlanDateList) {
        String str = "";
        if (CollectionUtils.isNotEmpty(empPlanDateList)) {
            List<String> dateList = new ArrayList<>();
            for (EmpPlanDate empPlanDate : empPlanDateList) {
                dateList.add(empPlanDate.getStartDate());
                dateList.add(empPlanDate.getEndDate());
            }
            dateList = dateList.stream().sorted().collect(Collectors.toList());
            String firstDate = dateList.get(0);
            String lastDate = dateList.get(dateList.size() - 1);
            if (firstDate.equals(lastDate)) {
                str = firstDate;
            } else {
                str = firstDate + "至" + lastDate;
            }
        }

        return str;
    }
    private static String checkWorkGroupName(List<EmpPlanDate> empPlanDateList, Map<String, WorkGroup> workGroupMap) {
        if (CollectionUtils.isNotEmpty(empPlanDateList)) {
            for (EmpPlanDate empPlanDate : empPlanDateList) {
                if (workGroupMap.containsKey(empPlanDate.getWorkGroupId())) {
                    return workGroupMap.get(empPlanDate.getWorkGroupId()).getName();
                }
            }
        }
        return "";
    }
}
